package ui_logic.logic;

import connect.*;
import game_logic.Controller;
import game_logic.GameFactory;

import javax.swing.*;
import java.util.Objects;

public class Receive {
    private String inputIP;
    private String msgContent;

    public int receiveDone = 0;

    public void sleep() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public String getInputIP() {
        return inputIP;
    }

    public void judgeMsgContent(String input, Client client, JPanel p) {
        switch (input) {
            case "对方拒绝了你的邀请":
                JOptionPane.showMessageDialog(p, "对方拒绝了你的邀请？", "可惜", JOptionPane.WARNING_MESSAGE);
                client.resetReceiveMsg();
                return;
            case "CjwCcqCblHdl=@-%^777>123":
                client.resetReceiveMsg();
                JOptionPane.showMessageDialog(p, "对方接受了你的邀请", "好耶", JOptionPane.INFORMATION_MESSAGE);
                receiveDone = 1;
//                client.callSendMessage(inputIP, "Link succeedCjwCcqCblHdl=@-%^777>123");
                gameStart(client, 0, true);
                return;
            case "Link succeedCjwCcqCblHdl=@-%^777>123":    //邀请方
                return;
            default:
                int userOption = JOptionPane.showConfirmDialog(p, "有人要和你下棋，是否接受？", "联机请求", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (userOption == JOptionPane.OK_OPTION) {
                    client.resetReceiveMsg();
                    client.callSendMessage(inputIP, "CjwCcqCblHdl=@-%^777>123");
                receiveDone = 1;
//                client.callSendMessage(inputIP, "CjwCcqCblHdl=@-%^777>123Link succeed");
                gameStart(client, 1, false);
                } else {
                    client.resetReceiveMsg();
                    client.callSendMessage(inputIP, "对方拒绝了你的邀请");
                }
        }
    }

    public void judgeMsgContentForBroadcast(String input, Broadcast broadcast, Client client, JPanel p) {
        switch (input) {
            case "对方拒绝了你的邀请":
                JOptionPane.showMessageDialog(p, "对方拒绝了你的邀请？", "可惜", JOptionPane.WARNING_MESSAGE);
                client.resetReceiveMsg();
                return;
            case "CjwCcqCblHdl=@-%^777>123":
                client.resetReceiveMsg();
                JOptionPane.showMessageDialog(p, "对方接受了你的邀请", "好耶", JOptionPane.INFORMATION_MESSAGE);
                client.callSendMessage(inputIP, "Link succeedCjwCcqCblHdl=@-%^777>123");
                receiveDone = 1;
                gameStart(client, 0, true);
                return;
            case "Link succeedCjwCcqCblHdl=@-%^777>123":    //被邀请
                receiveDone = 1;
//                client.callSendMessage(inputIP, "CjwCcqCblHdl=@-%^777>123Link succeed");
                gameStart(client, 1, false);
                return;
            default:
                int userOption = JOptionPane.showConfirmDialog(p, "有人要和你下棋，是否接受？", "联机请求", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (userOption == JOptionPane.OK_OPTION) {
                    broadcast.setBroadFinish();
                    broadcast.callSendBroadMessage("有人找我了");
                    client.resetReceiveMsg();
                    client.callSendMessage(inputIP, "CjwCcqCblHdl=@-%^777>123");
                } else {
                    client.resetReceiveMsg();
                    client.callSendMessage(inputIP, "对方拒绝了你的邀请");
                }
        }
    }

    public void receiveSingle(Client client, JPanel p) {
        String getMsg = client.getReceiveMsg();
//        System.out.println(getMsg);
        if (!Objects.equals(getMsg, "HdlCblCcqCjw")) {
            inputIP = getMsg.substring(0, getMsg.indexOf(':'));
            msgContent = getMsg.substring(getMsg.lastIndexOf(':') + 2);    //+2为了在再跳过一个空格
            System.out.println(msgContent);
            if (!inputIP.equals("127.0.0.1")) {
                judgeMsgContent(msgContent, client, p);
            } else {
                client.resetReceiveMsg();
                JOptionPane.showMessageDialog(p, "检测到主机IP输入，请重新输入", "自娱自乐请移步", JOptionPane.WARNING_MESSAGE);
            }
        }
    }

    public void receiveBroad(Broadcast broadcast, Client client, JPanel p) {
        String getMsg = client.getReceiveMsg();
//        System.out.println(getMsg);
        if (!Objects.equals(getMsg, "HdlCblCcqCjw")) {
            inputIP = getMsg.substring(0, getMsg.indexOf(':'));
            msgContent = getMsg.substring(getMsg.lastIndexOf(':') + 2, getMsg.length());    //+2为了在再跳过一个空格
            System.out.println(msgContent);
            if (!inputIP.equals("127.0.0.1")) {
                judgeMsgContentForBroadcast(msgContent, broadcast, client, p);
            } else {
                client.resetReceiveMsg();
                JOptionPane.showMessageDialog(p, "检测到主机IP输入，请重新输入", "自娱自乐请移步", JOptionPane.WARNING_MESSAGE);
            }
        }
    }

    public void gameStart(Client client, int own, boolean b) {
        Controller controllerS = GameFactory.makeGame(inputIP, b, true, client, own);
        controllerS.gameStart();
    }
}
